package com.searchparking.BUS;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.google.android.gms.maps.model.LatLng;
import com.google.android.maps.GeoPoint;
import com.searchparking.DTO.InfoObject;

public class ParseJsonData {
	private static final String TAG_DATA = "data";
    private static final String TAG_PARKING_ID = "ParkId";
    private static final String TAG_STATUS = "Status";
    private static final String TAG_LATI = "Latitude";
    private static final String TAG_LONG = "Longtitude";
    private static final String TAG_PARKING_NAME = "ParkName";
    private static final String TAG_PARKING_ADDR = "Address";
    public static  ArrayList<InfoObject> aObject;
    public ParseJsonData()
    {
    	aObject =  new ArrayList<InfoObject>();
    }
    public void parseData(JSONObject json) throws JSONException
    {
    	JSONArray  parkingList = json.getJSONArray(TAG_DATA);
    	for (int i = 0; i < parkingList.length(); i++) {
            JSONObject c = parkingList.getJSONObject(i);
             //        Log.d("IN",c.toString());
            // Storing each json item in variable
            String id =     c.getString(TAG_PARKING_ID);
            String name = c.getString(TAG_PARKING_NAME);
            String addr = c.getString(TAG_PARKING_ADDR);
            String lati = c.getString(TAG_LATI);
            String longi = c.getString(TAG_LONG);
            int status =  Integer.parseInt(c.getString(TAG_STATUS));
           // Log.d("PARKING_NAME",name);
            InfoObject parking = new InfoObject(id, name, status, lati, longi, addr);
            aObject.add(parking);
           
        }
    	
    }
    public ArrayList<InfoObject> getListFromCurrent(double numberKm,LatLng current) {
    	
    	ArrayList<InfoObject> aInfoParking = new ArrayList<InfoObject>();
    	int number = this.aObject.size();
    	for( int i=0;i<number;i++){
    		InfoObject infoParking = this.aObject.get(i);
    		Double latitude = Double.parseDouble(infoParking.getLati());
    		Double longitude = Double.parseDouble(infoParking.getLongi());
    		LatLng destinate = new LatLng(latitude, longitude) ;
    		int d=distance(current,destinate);
    		Log.d("KHOANGCAHC"+String.valueOf(i),String.valueOf(d));
    		if(d<(int)numberKm)
    		{
    			aInfoParking.add(infoParking);
    		}
    	}
    	Log.d("NumberParking",Integer.toString(aInfoParking.size()));
    	return aInfoParking;
    }
    public double  distanceCaculate(GeoPoint start, GeoPoint end)
    {
    	 double Radius = 6371;
    	 double lat1 = start.getLatitudeE6()/1E6;
         double lat2 = end.getLatitudeE6()/1E6;  
         double lon1 = start.getLongitudeE6()/1E6;  
         double lon2 = end.getLongitudeE6()/1E6;  
         double dLat = Math.toRadians(lat2-lat1);
         double dLon = Math.toRadians(lon2-lon1);
         double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
         Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
         Math.sin(dLon/2) * Math.sin(dLon/2);
         double c = 2 * Math.asin(Math.sqrt(a));
         double valueResult= Radius*c;
         double km=valueResult/1;
         DecimalFormat newFormat = new DecimalFormat("####");
         int kmInDec =  Integer.valueOf(newFormat.format(km));
         double meter=valueResult%1000;
         int  meterInDec= Integer.valueOf(newFormat.format(meter));
         Log.i("Radius Value",""+valueResult+"   KM  "+kmInDec+" Meter   "+meterInDec);

         return Radius * c;
    }
    public int distance(LatLng start, LatLng end)
    {
    	String dt="";
    	int d=0;
    	ParkingInfo parking = new ParkingInfo();
		try {
			parking.getDirectionsUrl(start,end);
			String data =parking.getDataUrl();
			dt=parking.getDistance(data);
			Log.d("Khoang cach km",dt);
			if(dt.contains("km"))
			{
				dt=dt.replace("km", "");
				d=(int)Float.parseFloat(dt.trim());
			}else if(dt.contains("m"))
			{
				dt=dt.replace("m", "");
				d=(int)(Float.parseFloat(dt.trim())/1000);
			}
				
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return d;
    }
     
    

}
